<template>
  <el-container>
    <el-main>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="reg-form">
        <el-form-item label="手机" prop="phone">
          <el-input placeholder="必填" type="text" v-model="ruleForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="change('phone')">修改手机</el-button>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="必填" type="text" v-model="ruleForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="change('username')">修改用户名</el-button>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input placeholder="必填" type="text" v-model="ruleForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="change('email')">修改邮箱</el-button>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input placeholder="必填" type="password" v-model="ruleForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="pass_repeat">
          <el-input placeholder="必填" type="password" v-model="ruleForm.pass_repeat" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="change('pass_repeat')">修改密码</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
export default {
  data () {
    let validatePassRepeat = (rule, value, callback) => {
      if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    let validatePhone = (rule, value, callback) => {
      let reg = 11 && /^((13|14|15|17|18)[0-9]{9})$/
      if (!reg.test(value)) {
        callback(new Error('请输入合法的手机号'))
      } else {
        callback()
      }
    }
    let user = this.$store.state.user
    return {
      ruleForm: {
        email: user.email,
        phone: user.phone,
        username: user.username,
        password: '',
        pass_repeat: ''
      },
      rules: {
        phone: [
          {required: true, message: '请输入手机号', trigger: 'change'},
          {validator: validatePhone, trigger: 'change'}
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'change'},
          {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'change'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'change'},
          {min: 6, max: 64, message: '长度在 6 到 64 个字符', trigger: 'change'},
          {type: 'email', message: '邮箱格式不合法', trigger: 'change'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'change'},
          {min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'change'}
        ],
        pass_repeat: [
          {required: true, message: '请输入密码', trigger: 'change'},
          {validator: validatePassRepeat, trigger: 'change'}
        ]}
    }
  },
  methods: {
    change: function (formName) {
      this.$refs['ruleForm'].validateField(formName,
        (valid) => {
          if (!valid) {
            return false
          }
        })
    }
  }
}
</script>

<style scoped>

</style>
